package tech.threekilogram.notificationtest;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Action;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat.DecoratedCustomViewStyle;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * @author liujin
 */
public class MainActivity extends AppCompatActivity {

      private static final String CHANNEL      = "MainChannel";
      private static final String CHANNEL_NAME = "TestChannel";

      private SeekBar mSeekBar;

      @Override
      protected void onCreate ( Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
            initView();
      }

      private void initView ( ) {

      }

      private String getChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_DEFAULT;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL, CHANNEL_NAME, importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return CHANNEL;
            } else {
                  return CHANNEL;
            }
      }

      private String getNoneChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_NONE;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL + "0", CHANNEL_NAME + "0", importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return CHANNEL + "0";
            } else {
                  return CHANNEL + "0";
            }
      }

      private String getMinChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_MIN;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL + "1", CHANNEL_NAME + "1", importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return CHANNEL + "1";
            } else {
                  return CHANNEL + "1";
            }
      }

      private String getLowChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_LOW;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL + "2", CHANNEL_NAME + "2", importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return CHANNEL + "2";
            } else {
                  return CHANNEL + "2";
            }
      }

      private String getHighChannel ( ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_HIGH;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL + "4", CHANNEL_NAME + "4", importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );
                  return CHANNEL + "4";
            } else {
                  return CHANNEL + "4";
            }
      }

      public void basicNotification ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a basic notification " )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 0, notification );
      }

      public void noneImportant ( View view ) {

            /* 什么都不显示 */

            String channel = getNoneChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a notification in IMPORTANCE_NONE channel" )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 2, notification );
      }

      public void minImportant ( View view ) {

            /* 只在下拉菜单显示 */

            String channel = getMinChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a notification in IMPORTANCE_MIN channel" )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 3, notification );
      }

      public void lowImportant ( View view ) {

            /* 同时在状态栏下拉菜单显示 */

            String channel = getLowChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a notification in IMPORTANCE_LOW channel" )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 4, notification );
      }

      public void highImportant ( View view ) {

            /* 同时在状态栏下拉菜单显示,播放声音,额外弹出窗口显示 */
            String channel = getHighChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a notification in IMPORTANCE_HIGH channel" )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 5, notification );
      }



      /* 似乎NotificationCompat.PRIORITY_XXX 没有效果 */

      public void maxPriority ( View view ) {

            String channel = getHighChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a PRIORITY_MAX notification " )
                .setPriority( NotificationCompat.PRIORITY_MAX )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 6, notification );
      }

      public void highPriority ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a PRIORITY_HIGH notification " )
                .setPriority( NotificationCompat.PRIORITY_HIGH )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 7, notification );
      }

      public void defaultPriority ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a PRIORITY_DEFAULT notification " )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 8, notification );
      }

      public void lowPriority ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a PRIORITY_LOW notification " )
                .setPriority( NotificationCompat.PRIORITY_LOW )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 9, notification );
      }

      public void minPriority ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a PRIORITY_MIN notification " )
                .setPriority( NotificationCompat.PRIORITY_MIN )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 10, notification );
      }

      public void contentTap ( View view ) {

            Intent intent = new Intent( this, DetailActivity.class );
            intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
            PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, 0 );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a content notification " )
                .setAutoCancel( true )
                .setContentIntent( pendingIntent )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 11, notification );
      }

      public void taskStackContent ( View view ) {

            Intent intent = new Intent( this, DetailActivity.class );
            intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create( this );
            taskStackBuilder.addParentStack( DetailActivity.class );
            taskStackBuilder.addNextIntent( intent );
            PendingIntent pendingIntent = taskStackBuilder
                .getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a TaskStack notification " )
                .setAutoCancel( true )
                .setContentIntent( pendingIntent )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 12, notification );
      }

      public void addAction ( View view ) {

            Intent intent = new Intent( this, TestReceiver.class );
            String action = "tech.threekilogram.notification.ACTION";
            intent.setAction( action );
            intent.putExtra( "extra", "Hello" );

            PendingIntent pendingIntent = PendingIntent.getBroadcast( this, 0, intent, 0 );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a action notification " )
                .setAutoCancel( true )
                .addAction( R.drawable.weibo, "test action", pendingIntent )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 13, notification );
      }

      public void inputAction ( View view ) {

            Intent intent = new Intent( this, InputReceiver.class );
            PendingIntent pendingIntent = PendingIntent
                .getBroadcast( this, 12, intent, PendingIntent.FLAG_UPDATE_CURRENT );

            RemoteInput remoteInput = new RemoteInput.Builder( "key_input" ).setLabel( "reply" )
                                                                            .build();

            Action action = new Action.Builder( R.drawable.replay, "replay", pendingIntent )
                .addRemoteInput( remoteInput )
                .build();

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a action notification " )
                .setAutoCancel( true )
                .addAction( action )
                .build();

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from( this );
            assert notificationManager != null;
            notificationManager.notify( 14, notification );
      }

      public void progress ( View view ) {

            if( mSeekBar == null ) {

                  mSeekBar = findViewById( R.id.seekBar );

                  String channel = getChannel();
                  Builder builder = new Builder( this, channel )
                      .setSmallIcon( R.drawable.egg_select )
                      .setContentTitle( "new Notification" )
                      .setContentText( "this is a progress notification " )
                      .setProgress( mSeekBar.getMax(), mSeekBar.getProgress(), false );

                  Notification notification = builder.build();

                  NotificationManagerCompat notificationManager = NotificationManagerCompat
                      .from( this );
                  assert notificationManager != null;
                  notificationManager.notify( 15, notification );

                  mSeekBar.setOnSeekBarChangeListener(
                      new ProgressChangeSeekBar( builder, notificationManager ) );
            }
      }

      public void lockScreen ( View view ) {

            String channel = getChannel();
            Builder builder = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a lock screen notification " )
                .setVisibility( Notification.VISIBILITY_PUBLIC );

            Notification notification = builder.build();

            NotificationManagerCompat notificationManager = NotificationManagerCompat
                .from( this );
            assert notificationManager != null;
            notificationManager.notify( 16, notification );
      }

      public void bigTextStyle ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "big text Notification" )
                .setContentText( "this is a big text notification " )
                .setStyle( new BigTextStyle().bigText( "曾经沧海难为水,除却巫山不是云.取次花丛懒回顾,半缘修道半缘君" ) )
                .setPriority( NotificationCompat.PRIORITY_DEFAULT )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 1, notification );
      }

      public void largeImage ( View view ) {

            Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.a704 );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a action notification " )
                .setAutoCancel( true )
                .setLargeIcon( bitmap )
                .setStyle( new BigPictureStyle().bigPicture( bitmap ).bigLargeIcon( null ) )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 17, notification );
      }

      public void inBox ( View view ) {

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a action notification " )
                .setAutoCancel( true )
                .setStyle(
                    new InboxStyle().addLine( "曾经沧海难为水" )
                                    .addLine( "除却巫山不是云" )
                                    .addLine( "取次花丛懒回顾" )
                                    .addLine( "半缘修道半缘君" )
                ).build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 18, notification );
      }

      public void toChannelSetting ( View view ) {

            if( VERSION.SDK_INT > VERSION_CODES.O ) {

                  int importance = NotificationManager.IMPORTANCE_DEFAULT;
                  /* 必须设置 importance */
                  NotificationChannel channel = new NotificationChannel(
                      CHANNEL, CHANNEL_NAME, importance );
                  channel.setDescription( "this is test notification channel" );
                  NotificationManager notificationManager = getSystemService(
                      NotificationManager.class );
                  assert notificationManager != null;
                  notificationManager.createNotificationChannel( channel );

                  Intent intent = new Intent( Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS );
                  intent.putExtra( Settings.EXTRA_APP_PACKAGE, getPackageName() );
                  intent.putExtra( Settings.EXTRA_CHANNEL_ID, channel.getId() );
                  startActivity( intent );
            }
      }

      public void remote ( View view ) {

            RemoteViews remoteViews = new RemoteViews( getPackageName(), R.layout.remote_00 );
            RemoteViews remoteViewsE = new RemoteViews( getPackageName(), R.layout.remote_01 );

            String channel = getChannel();
            Notification notification = new Builder( this, channel )
                .setSmallIcon( R.drawable.egg_select )
                .setContentTitle( "new Notification" )
                .setContentText( "this is a action notification " )
                .setAutoCancel( true )
                .setStyle( new DecoratedCustomViewStyle() )
                .setCustomContentView( remoteViews )
                .setCustomBigContentView( remoteViewsE )
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE );
            assert notificationManager != null;
            notificationManager.notify( 19, notification );
      }

      private class ProgressChangeSeekBar implements OnSeekBarChangeListener {

            private NotificationCompat.Builder mBuilder;
            private NotificationManagerCompat  mManagerCompat;

            ProgressChangeSeekBar (
                Builder builder, NotificationManagerCompat managerCompat ) {

                  mBuilder = builder;
                  mManagerCompat = managerCompat;
            }

            @Override
            public void onProgressChanged ( SeekBar seekBar, int progress, boolean fromUser ) {

                  mBuilder.setProgress( seekBar.getMax(), seekBar.getProgress(), false );
                  mManagerCompat.notify( 15, mBuilder.build() );
            }

            @Override
            public void onStartTrackingTouch ( SeekBar seekBar ) {

            }

            @Override
            public void onStopTrackingTouch ( SeekBar seekBar ) {

                  if( seekBar.getProgress() == seekBar.getMax() ) {
                        mBuilder.setContentText( "complete" ).setProgress( 0, 0, false );
                        mManagerCompat.notify( 15, mBuilder.build() );
                  }
            }
      }
}
